package com.xuecheng.manage_cms_client.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {

    /**
     * 1、创建“ex_cms_postpage”交换机
     * 2、每个Cms Client创建一个队列与交换机绑定
     * 3、每个Cms Client程序配置队列名称和routingKey，将站点ID作为routingKey。
     */
    //首页队列bena的名称
    public static final String QUEUE_CMS_PAGE_POSTPAGE = "queue_cms_page_postpage";
    //交换机的名称
    public static final String EX_ROUTING_CMS_PAGE_POSTPAGE = "ex_routing_cms_postpage";
    //课程队列bena的名称
    public static final String QUEUE_CMS_COURSE_POSTPAGE = "queue_cms_course_postpage";
    //队列的名称
    @Value("${xuecheng.mq.page.queue}")
    public String queue_cms_postpage_name;
    //routingKey 即站点Id
    @Value("${xuecheng.mq.page.routingKey}")
    public String routingPageKey;
    //课程队列的名称
    @Value("${xuecheng.mq.course.queue}")
    public String queue_cms_postpage_name_course;
    //课程routingKey 即站点Id
    @Value("${xuecheng.mq.course.routingKey}")
    public String routingPageKeyCourse;

    /**
     * 交换机配置使用direct
     * @return
     */
    @Bean(EX_ROUTING_CMS_PAGE_POSTPAGE)
    public Exchange exchangeTopicsInform(){
        return ExchangeBuilder.directExchange(EX_ROUTING_CMS_PAGE_POSTPAGE).durable(true).build();
    }
    //声明首页队列
    @Bean(QUEUE_CMS_PAGE_POSTPAGE)
    public Queue queueCmsPagePostPage(){
        Queue queue = new Queue(queue_cms_postpage_name);
        return queue;
    }
    //声明课程队列
    @Bean(QUEUE_CMS_COURSE_POSTPAGE)
    public Queue queueCmsCoursePostPage(){
        Queue queue = new Queue(queue_cms_postpage_name_course);
        return queue;
    }
    /**
     * 绑定首页队列到交换机
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding bingingQueueInformSms(@Qualifier(QUEUE_CMS_PAGE_POSTPAGE) Queue queue,
                                         @Qualifier(EX_ROUTING_CMS_PAGE_POSTPAGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(routingPageKey).noargs();
    }
    /**
     * 绑定课程队列到交换机
     * @param queue
     * @param exchange
     * @return
     */
    @Bean
    public Binding bingingQueueCourseSms(@Qualifier(QUEUE_CMS_COURSE_POSTPAGE) Queue queue,
                                         @Qualifier(EX_ROUTING_CMS_PAGE_POSTPAGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(routingPageKeyCourse).noargs();
    }
}
